package com.lark.web.service.impl;

import java.util.List;

import com.github.dockerjava.api.DockerClient;
import com.github.pagehelper.PageHelper;
import com.lark.common.core.page.PageDomain;
import com.lark.common.utils.StringUtils;
import com.lark.common.utils.sql.SqlUtil;
import com.lark.web.docker.service.DockerUtil;
import com.lark.web.domain.StorageVolume;
import com.lark.web.service.HostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.lark.common.core.domain.BaseEntity;
import com.lark.common.core.service.BaseServiceImpl;
import com.lark.web.mapper.ContainerMapper;
import com.lark.web.domain.Container;
import com.lark.web.service.ContainerService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 容器Service业务层处理
 * @author fei
 * @email 952597665@qq.com
 * @date 2024-08-13
 */
@Service
@Transactional(readOnly = true)
public class ContainerServiceImpl extends BaseServiceImpl<ContainerMapper, Container> implements ContainerService {

    private static final Logger log = LoggerFactory.getLogger(ContainerServiceImpl.class);
    @Autowired
    private HostService hostService;

    /**
     * 获取单条数据
     * @param container 容器
     * @return 容器
     */
    @Override
    public Container get(Container container) {
        Container dto = super.get(container);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 容器id
     * @return 容器
     */
    @Override
    public Container get(String id) {
        Container dto = super.get(id);
        return dto;
    }

    /**
     * 查询容器列表
     * @param container 容器
     * @return 容器
     */
    @Override
    public List<Container> findList(Container container) {
		List<Container> containerList = super.findList(container);
        return containerList;
    }

    /**
     * 分页查询容器列表
     * @param container 容器
     * @return 容器
     */
    @Override
    public PageInfo<Container> findPage(Container container) {
        DockerClient client = hostService.buildClient(container.getIp());
        List<Container> containerList = DockerUtil.getContainers(client);

        PageDomain page = container.getPage();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
            Boolean reasonable = page.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
        return new PageInfo(containerList);
    }

    /**
     * 保存容器
     * @param container
     * @return 结果
     */
    @Override
    public boolean save(Container container) {
        return super.save(container);
    }

    /**
     * 删除容器信息
     * @param container
     * @return 结果
     */
    @Override
    public boolean remove(Container container) {
        return super.remove(container);
    }

    /**
     * 批量删除容器
     * @param ids 需要删除的容器ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteContainerByIds(String[] ids) {
        return mapper.deleteContainerByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

}
